<template>
  <div class="attentionds">
    <div class="attentiondsTit">
      <div class="attentionBox">
        <div class="">
          <div class="searchRight">
            <span
              v-if="!isEdit"
              class="batch"
              @click="showEdit"
            >批量管理</span>
            <div v-else class="saveList">
              <el-checkbox v-model="selectAll" @change="changeChecked">全选</el-checkbox>
              <span class="delete" @click="cancelFun">取消收藏</span>
              <span class="save" @click="saveList">保存</span>
            </div>
            <el-input
              v-if="activeName==='commodity'"
              type="text" v-model="query.keyword"
              maxlength="20"
              @keyup.enter="searchPro"
              placeholder="请输入搜索商品"
            />
            <el-input
              v-else
              type="text"
              v-model="query.keyword"
              maxlength="20"
              @keyup.enter="searchPro"
              placeholder="请输入搜索店铺"
            >
              <span class="searchBtn" @click="searchPro">
                <svg-icon name="search" style="width: 30px;height: 30px;" />
              </span>
            </el-input></div>
        </div>
      </div>
      <el-tabs
        v-model="activeName"
        @tab-click="tabChange"
      >
        <el-tab-pane label="收藏的商品" name="commodity">
          <div class="list sub-main" v-if="attentiondList?.length" v-loading="listLoading">
            <div class="attentiondsList">
              <div class="listBox">
                <div
                  class="attentiondsItem"
                  v-for="(item,index) of attentiondList"
                  :key="item.collectId"
                  :class="{
                    selected: item.selected, isEdit: isEdit
                  }"
                  @click="selectPro(index,item.selected)"
                >
                  <div class="mc" v-if="isEdit"></div>
                  <div class="imgBox" :class="{offShelf: item.shelveState === 0}">
                    <div class="tipText" v-if="item.shelveState === 0">
                      <span>已下架</span>
                    </div>
                    <img :src="item.image" alt="" />
                  </div>
                  <div class="info">
                    <h3 class="overflow">{{ item.productName }}</h3>
                    <span class="price">¥{{ item.price }}</span>
                  </div>
                  <div class="btnBox">
                    <span @click="cancelSingle(item.collectId)">取消收藏</span>
                    <span @click="toProductDetail(item)">查看详情</span>
                  </div>
                </div>
                <div class="clearfix"></div>
              </div>
            </div>
            <el-pagination
              :hide-on-single-page="true"
              background
              layout="prev, pager, next, jumper"
              :current-page="page.page"
              :page-size="10"
              :total="page.total"
              @current-change="handleCurrentChange"
            />
          </div>
          <div class="nothing sub-main" v-else>
            <svg-icon style="width: 240px; height: 240px; margin-bottom: 20px;" name="user-favorite-nodata" />
            <p class="fs20 font-color-999">你还没有收藏的商品～</p>
          </div>
        </el-tab-pane>
        <el-tab-pane label="收藏的店铺" name="shop">
          <div
            class="list sub-main"
            v-if="shopList?.length"
            v-loading="listLoading"
          >
            <div class="attentiondsList">
              <div class="listBox">
                <div
                  class="attentiondsItem"
                  v-for="(item,index) of shopList"
                  :key="item.id"
                  :class="{
                    selected: item.selected,
                    isEdit: isEdit
                  }"
                  @click="selectShop(index,item.selected)"
                >
                  <div class="mc" v-if="isEdit"></div>
                  <div class="imgBox">
                    <img :src="item.shopLogo" alt="" />
                  </div>
                  <div class="info">
                    <h3>{{ item.shopName }}</h3>
                  </div>
                  <div class="btnBox">
                    <span @click="cancelSingle(item.collectId)">取消收藏</span>
                    <span @click="toStore(item.shopId)">进入店铺</span>
                  </div>
                </div>
                <div class="clearfix"></div>
              </div>
            </div>
            <el-pagination
              :hide-on-single-page="true"
              background
              layout="prev, pager, next, jumper"
              :current-page="page.page"
              :page-size="page.pageSize"
              :total="page.total"
              @current-change="handleCurrentChange"
            />
          </div>
          <div class="nothing sub-main" v-else>
            <svg-icon style="width: 240px; height: 240px; margin-bottom: 20px;" name="user-favorite-nodata" />
            <p class="fs20 font-color-999">你还没有收藏的店铺～</p>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup>
import { defineComponent, reactive, ref, shallowRef, onBeforeMount } from 'vue'
import { useRouter } from 'vue-router';
import SvgIcon from '@/components/svgIcon/index.vue';
import {
  getCollectShop,
  getCollectProduct,
  cancelCollect
} from '@/api/user/user.js'
defineComponent({
  name: 'Favorites'
})

const router = useRouter();

/**
 * 内容查询
 */
let page = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})
let query = reactive({
  keyword: ''
})
let activeName = shallowRef('commodity');
let attentiondList = ref([]);
let shopList = ref([]);
let listLoading = shallowRef(false);

function getAllShop () {
  listLoading.value = true;
  getCollectShop({
    page: page.page,
    pageSize: page.pageSize,
    search: query.keyword
  }).then(response => {
    const res = response.data;
    if (res.code === "200") {
      shopList.value = res.data && res.data.list || [];
      page.total = res.data && parseInt(res.data.total) || 0;
    }
  }).catch(err => {
    ElMessage.warning(err.message)
  }).finally(() => {
    listLoading.value = false;
  })
}

// 收藏商品查询
function getAllProduct () {
  listLoading.value = true
  getCollectProduct({
    page: page.page,
    pageSize: page.pageSize,
    search: query.keyword
  }).then(response => {
    const res = response.data;
    if (res.code === "200") {
      attentiondList.value = res.data && res.data.list || [];
      page.total = res.data && res.data.total || 0;
    }
  }).catch(err => {
    ElMessage.warning(err.message)
  }).finally(() => listLoading.value = false)
}
function handleCurrentChange (val) {
  page.page = val;
  searchFn();
}
/** tabs内容切换 */
function tabChange (tab) {
  activeName.value = tab.paneName
  page.page = 1;
  query.keyword = "";
  isEdit.value = false;
  selectAll.value = false;
  searchFn();
}
// 搜索
function searchPro () {
  page.page = 1;
  searchFn();
}

function searchFn () {
  if (activeName.value === "commodity") {
    getAllProduct();
  }
  else {
    getAllShop();
  }
}

/**
 *
 */
let selectAll = shallowRef(false);
let isEdit = shallowRef(false);

// 商品单个选中
function selectPro (index, selected) {
  attentiondList.value[index].selected = selected ? 0 : 1
  selectAll.value = true;
  attentiondList.value.map(item => {
    if (item.selected === 0) {
      selectAll.value = false;
    }
  });
}
// 店铺单个选中
function selectShop (index, selected) {
  shopList.value[index].selected = selected ? 0 : 1
  selectAll.value = true;
  shopList.value.map(item => {
    if (item.selected === 0) {
      this.selectAll = false;
    }
  });
}
// 全选
function changeChecked () {
  if (activeName.value === "commodity") {
    attentiondList.value.forEach(item => {
      item.selected = selectAll.value
    });
  }
  else {
    shopList.value.forEach(item => {
      item.selected = selectAll.value
    });
  }
}
// 批量管理
function showEdit () {
  if (activeName.value === "commodity") {
    if (attentiondList.value.length === 0) {
      return;
    }
  }
  else {
    if (shopList.value.length === 0) {
      return;
    }
  }
  isEdit.value = true;
}
function saveList () {
  if (activeName.value === "commodity") {
    attentiondList.value.forEach(item => {
      item.selected = false // false/0
    });
  }
  else {
    shopList.value.forEach(item => {
      item.selected = false // false/0
    });
  }
  selectAll.value = false;
  isEdit.value = false;
}

// 取消单个收藏
function cancelSingle (id) {
  unfavorite([id]);
}

// 取消收藏请求
function unfavorite (ids) {
  cancelCollect({ ids: ids }).then(response => {
    const res = response.data;
    if (res.code === "200") {
      ElMessage.success("取消成功");
      isEdit.value = false;
      selectAll.value = false
      if (activeName.value === "commodity") {
        getAllProduct();
      }
      else {
        getAllShop();
      }
    }
  })
}
// 取消选中收藏
function cancelFun () {
  let flag2 = false;
  let ids = [];
  if (activeName.value === "commodity") {
    attentiondList.value.forEach(item => {
      if (item.selected) {
        flag2 = true;
        ids.push(item.collectId);
      }
    });
    if (flag2 === false) {
      ElMessage.warning("请先选择需要取消的收藏");
      return
    }
  }
  else {
    shopList.value.forEach(item => {
      if (item.selected) {
        flag2 = true;
        ids.push(item.collectId);
      }
    });
    if (!flag2) {
      ElMessage.warning("请先选择需要取消的收藏");
      return
    }
  }
  ElMessageBox.confirm(
    "此操作将取消选中的收藏, 是否继续?",
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    }
  ).then(() => {
    unfavorite(ids);
  }).catch(() => {
  });
}


// 跳转到商品详情
function toProductDetail (item) {
  router.push({
    path: "/productDetail",
    query: {
      proData: JSON.stringify({
        shopId: item.shopId,
        skuId: item.skuId,
        productId: item.productId
      })
    }
  });
}
// 跳转到店铺
function toStore (id) {
  router.push({
    path: "/store",
    query: { shopId: id }
  });
}
onBeforeMount(() => {
  getAllProduct();
})
</script>

<style lang="scss" scoped>
$searchHeight: 35px;
.attentionds {
  border: 1px solid #E5E5E5;
  .attentiondsTit {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    position: relative;
    :deep(.el-tabs__nav-wrap::after){
      height: 1px;
    }
    :deep(.el-tabs--top){
      width: 100%;
    }
    .attentionBox {
      position: absolute;
      top: 6px;
      right: 4px;
      z-index: 99;
      .searchRight {
        display: flex;
        .el-input {
          width: 280px;
          padding-left: 10px;
          font-size: 14px;
          border: 2px solid #F3F4F5;
        }
        span {
          display: block;
          height: 30px;
          line-height: 30px;
        }
        .batch {
          color: #FFFFFF;
          font-size: 12px;
          width: 78px;
          text-align: center;
          height: $searchHeight;
          line-height: $searchHeight;
          background: $mainGlod;
          cursor: pointer;
        }
        .searchBtn {
          // width: 64px;
          height: $searchHeight;
          line-height: $searchHeight;
          // background: $mainGlod;
          color: #FFFFFF;
          font-size: 25px;
          text-align: center;
          cursor: pointer;
          position: absolute;
          right: 10px;
        }
      }
    }
    span {
      display: block;
      height: 30px;
      line-height: 30px;
    }
    .saveList {
      display: flex;
      align-items: center;
      span {
        margin-left: 36px;
        cursor: pointer;
        color: #333333;
        font-size: 14px;
      }
      span.save {
        width: 104px;
        height: $searchHeight;
        background: #FFFFFF;
        color: #333333;
        border: 1px solid $mainGlod;
        text-align: center;
        line-height: $searchHeight;
      }
    }
    :deep(.el-tabs__nav-scroll){
      padding: 0 25px;
      height: 50px;
      line-height: 50px;
      background-color: #FAFAFA;
    }
    :deep(.el-tabs__item) {
      height: 50px;
    }
  }
  .list {
    padding: 10px 20px 20px 20px;
    .attentiondsList {
      .topTime {
        color: #333333;
        font-size: 18px;
        margin-bottom: 30px;
      }
      .listBox {
        .attentiondsItem {
          width: 186px;
          float: left;
          margin:0 18px 20px 0;
          position: relative;
          .imgBox {
            width: 186px;
            height: 186px;
            border: 1px solid #E5E5E5;
            border-bottom: 0;
            box-sizing: border-box;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .info {
            border: 1px solid #E5E5E5;
            box-sizing: border-box;
            text-align: center;
            padding: 0 15px 15px;
            h3 {
              font-size: 14px;
              color: #333333;
              height: 30px;
              line-height: 30px;
              margin-top: 10px;
            }
            span {
              color: $mainGlod;
              font-size: 14px;
            }
          }
          .btnBox {
            border: 1px solid #E5E5E5;
            display: flex;
            border-top: none;
            span {
              width: 50%;
              display: block;
              text-align: center;
              font-size: 14px;
              color: #333333;
              height: 30px;
              line-height: 30px;
              cursor: pointer;
            }
            span:first-child {
              border-right: 1px solid #E5E5E5;
              box-sizing: border-box;
            }
          }
          .offShelf {
            position: relative;
            z-index: 99;
            .tipText {
              top: 0;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
              display: flex;
              align-items: center;
              justify-content: center;
              background: rgba(28,28,28,0.5);
              color: #fff;
            }
          }
        }
        .attentiondsItem:nth-child(5n) {
          margin-right: 0;
        }
        .isEdit:before {
          content: "";
          background: url("./../../../assets/images/user-unselected.svg");
          width: 24px;
          height: 24px;
          position: absolute;
          right: 10px;
          top: 10px;
          display: block;
          background-size: contain;
          z-index: 999;
        }
        .selected:before {
          background: url("./../../../assets/images/user-selected.svg");
          background-size: contain;
        }
      }
    }
    :deep(.el-pagination){
      margin: 30px 0 20px 0;
    }
  }
  .sub-main{
    min-height: 400px;
  }
  .nothing{
    width: 100%;
    margin-top: 30px;
    text-align: center;
    min-height: 400px;
    p{
        margin-bottom: 20px;
    }
    .el-button{
        background-color: $mainGlod;
        color: #FFFFFF;
        font-weight: normal;
        border-radius: 0;
    }
  }
  .mc{
    width: 100%;
    height: 100%;
    position: absolute;
    opacity: 0;
  }
}
</style>
